<?php


	namespace Admin\Model;
	use \Think\Model;

	class PropertyModel extends Model{


		protected $tableName = 'property';
		// protected $fields = ['id','name'];

		protected $_validate = [
                              	 ['property','requier','属性名重复定义了!',1,'callback'],
							  ];

		/**
		 * 检查属性名是否在该分类表下重复定义
		 */
		public function checkProperty($pro){
			if(empty($pro)){
				return ['mess'=>"属性名称不能为空!",'code'=>1];
			}else{
				if($this -> where(['name'=>$pro]) -> select()){
					return ['mess'=>"属性名称已经存在!",'code'=>1];
				}else{
					return ['mess'=>"属性名可以注册!",'code'=>0];
				}
			}
			
		}

		public function checkcatPro($cid,$pro){
			if($this -> alias('pro') -> join('__CAT_PRO__ cp on pro.id=cp.proId') -> where(['pro.name'=>$pro,'cp.catId'=>$cid])->select()){
				return ['mess'=>"属性名称已经存在!",'code'=>1];
			}else{
				return ['mess'=>"属性名可以注册!",'code'=>0];
			}
		}

		// public function checkProval($val){
		// 	$
		// }

		/**
		 * $cid  分类id
		 * $pro  属性名
		 * $value 属性值名
		 */
		public function addPropery($cid,$pro,$value){
			$catModel = M('cat_pro');
			$valModel = M('proval');

			//先插入属性表
			$this  -> startTrans();

			//检查有没有该属性
			if($this -> alias('pro') -> join('__CAT_PRO__ cp on pro.id=cp.proId') -> where(['pro.name'=>$pro,'cp.catId'=>$cid])->select()){
				return ['error'=>'你已经该该分类中添加了该属性','flag'=>false];
			}else{

				if($this -> add(['name'=>$pro])){
					$proId = $this -> getLastInsID();

					//再插入分类属性表
					$catModel -> startTrans();
					if($catModel -> add(['catId'=>$cid,'proId'=>$proId])){

						$valModel->startTrans();

						//再插入属性值表
						$arr = [];
						foreach($value as $k => $val){
						 	$arr[$k] = ['name'=>$val,'proId'=>$proId];
						}

						if($valModel -> addAll($arr)){

							$valModel -> commit();
							return  ['mess','flag'=>true];
						}else{
							$this -> rollback();
							$catModel -> rollback();
							$valModel -> rollback();
							return ['error'=>'插入属性值表失败','flag'=>false]; 
						}
						
					}else{
						$this -> rollback();
						$catModel -> rollback();
						return ['error'=>'插入分类属性表失败','flag'=>false];
					}
				}else{
					$this -> rollback();
					return ['error'=>'插入属性表失败','flag'=>false];
				}

		   }
	   }

	   /**
	    * [delPro 多表连删操作]
	    * @param  [type] $pro [属性id]
	    * @return [type]      [影响行数]
	    */
	   public function delPro($pro){
	   	  $sql = 'delete cp,pro,proval from cat_pro cp inner join property pro on pro.id=cp.proId left join proval on proval.proId=pro.id where pro.id='.$pro;
	   	  return $this -> execute($sql);
	   }


	   public function addGoods($data){
	   		$time = time();
	   		$arr = [];

	   		//向goods表中添加数据
	   		M('goods') -> startTrans();
	   		M('goods_sku')->startTrans();
	   		M('spu_sku')->startTrans();
	   		M('goodsdetail')->startTrans();
	   		M('goods_brand')->startTrans();
	   		M('store')->startTrans();
	   		M('goodsimg')->startTrans();

	   	    if(!M('goods')-> add(['catId'=>$data['cid'],'addtime'=>$time,'name'=>$data['goodsname']])){
	   	    	M('goods')->rollback();
	   	    	return ['mess'=>M('goods')->getError(),'flag'=>false];

	   	    }else{

	   	    	$spuId = M('goods') -> getLastInsID();
		   	 	$skuId = M('goods_sku') -> Max('skuId')+1; 
		   	 	foreach($data['val'] as $key => $val){
		   	 		$arr[]= ['skuId'=>$skuId,'proId'=>$data['property'][$key],'proValId'=>$val];
		   	 	}

		   	 	//向goods_sku表中添加信息
		   	 	if(!M('goods_sku') -> addAll($arr)){
		   	 		M('goods')->rollback();
		   	 		M('goods_sku')->rollback();
		   	 		return ['mess'=>M('goods_sku')->getError(),'flag'=>false];
		   	 	}else{

		   	 		//向spu_sku关系表中添加数据
		   	 		if(!M('spu_sku') -> add(['spuId'=>$spuId,'skuId'=>$skuId])){
		   	 			M('spu_sku')->rollback();
						M('goods')->rollback();
		   	 			M('goods_sku')->rollback();
		   	 			return ['mess'=>M('spu_sku')->getError(),'flag'=>false];
		   	 		}else{

		   	 			//向goodsdetail表中添加数据
		   	 			if(!M('goodsdetail')-> add(['goodsName'=>$data['goodsname'],'skuId'=>$skuId ,'price'=>$data['price'],'status'=>$data['status'],'isHot'=>0,'SaleNumber'=>0])){
		   	 				M('goodsdetail')->rollback();
		   	 				M('spu_sku')->rollback();
							M('goods')->rollback();
			   	 			M('goods_sku')->rollback();
			   	 			return ['mess'=>M('goodsdetail')->getError(),'flag'=>false];
		   	 			}else{

		   	 				//向goodsBrand表中添加数
		   	 				if(!M('goods_brand')->add(['goodsId'=>$spuId,'brandId'=>$data['brand']])){
		   	 					M('goods_brand')->rollback();
		   	 					M('goodsdetail')->rollback();
			   	 				M('spu_sku')->rollback();
								M('goods')->rollback();
				   	 			M('goods_sku')->rollback();
				   	 			return ['mess'=>M('goods_brand')->getError(),'flag'=>false];
		   	 				}else{
		   	 					if(!M('store') -> add(['skuId'=>$skuId,'store'=>$data['store']])){
		   	 						M('store') ->rollback();
		   	 						M('goods_brand')->rollback();
			   	 					M('goodsdetail')->rollback();
				   	 				M('spu_sku')->rollback();
									M('goods')->rollback();
					   	 			M('goods_sku')->rollback();
					   	 			return ['mess'=>M('store')->getError(),'flag'=>false];
		   	 					}else{
		   	 						if(!M('goodsimg') -> add(['imgUrl'=>$data['imgUrl'],'isMain'=>1,'spuId'=>$spuId] )){
		   	 							M('goodsimg')->rollback();
		   	 							M('store') ->rollback();
			   	 						M('goods_brand')->rollback();
				   	 					M('goodsdetail')->rollback();
					   	 				M('spu_sku')->rollback();
										M('goods')->rollback();
						   	 			M('goods_sku')->rollback();
						   	 			return ['mess'=>M('store')->getError(),'flag'=>false];
		   	 						}else{
		   	 							M('goodsimg')->rollback();
		   	 							M('store') ->commit();
			   	 						M('goods_brand')->commit();
				   	 					M('goodsdetail')->commit();
					   	 				M('spu_sku')->commit();
										M('goods')->commit();
						   	 			M('goods_sku')->commit();
		   	 							return ['mess'=>M('store')->getError(),'flag'=>true,'spuid'=>$spuId];
		   	 						}
		   	 					}
		   	 				}	
		   	 			}
		   	 		}


		   	 	}

	   	    }

	   	 	   	 	
	   }
	}